﻿
@{
    ViewData["Title"] = "类别列表";
    Layout = "~/Views/Shared/_Back.cshtml";
}
@model IEnumerable<IVBlog.Models.Category>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <h2>添加新类别目录</h2>
                    <div class="layui-card">
                        <div class="layui-card-body">
                            <form action="/Back/TypeAdd" method="post" class="layui-form">
                                <input type="text" name="txtName" id="txtName" required lay-verify="required" placeholder="请输入添加的类别名称" autocomplete="off" class="layui-input">
                                <button type="submit" class="layui-btn" style="margin-top:30px">添加</button>
                                @if (TempData["error"] != null || TempData["success"] !=null)
                                {
                                    <h5 style="color:red">@TempData["error"]@TempData["success"]</h5>
                                }
                            </form>
                        </div>
                    </div>
                </div>
                <div class="layui-card-body layui-table-body layui-table-main">
                    <h2>所有类别</h2>
                    <table class="layui-table layui-form">
                        <thead>
                            <tr>
                                <th>类别编号</th>
                                <th>类别名称</th>
                                <th>文章数目</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            @foreach (var item in Model)
                            {
                                <tr>
                                    <td>@item.CategoryId</td>
                                    <td>@item.Cname</td>
                                    <td>@item.Cnums</td>
                                    <td class="td-manage">
                                        <div class="layui-btn-group">
                                            <button type="button" class="layui-btn layui-btn-sm layui-disabled" disabled><i class="layui-icon">&#xe654;</i>编辑</button>
                                            <button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="Del(@(item.CategoryId))"><i class="layui-icon">&#xe640;</i>删除</button>
                                        </div>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>
                </div>
                <div class="layui-card-body">
                    <div class="page text-center">
                        <div id="pagecard">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
@section Scripts{
    <script>
        layui.use('laypage', function () {
            var laypage = layui.laypage;
            var totalRows = @ViewBag.totalRows;
            var totalPage = @ViewBag.totalPage;
            var pageIndex = @ViewBag.pageIndex;
            var pageSize = @ViewBag.pageSize;
            //执行一个laypage实例
            laypage.render({
                elem: 'pagecard' //注意，这里的 test1 是 ID，不用加 # 号
                , count: totalRows //数据总数，从服务端得到
                , limit: pageSize //每页显示的条数
                , curr: pageIndex
                , jump: function (obj, first) {
                    //obj包含了当前分页的所有参数，比如：
                    console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
                    console.log(obj.limit); //得到每页显示的条数
                    //首次不执行
                    if (!first) {
                        if (pageIndex != obj.curr) {
                            location.href = "../Back/TypeList?pageIndex=" + obj.curr + "&pageSize=" + obj.limit;
                        }
                    }
                }
            });
        });
        function Del(DynamicId) {
            var layer = layui.layer;
            layer.confirm('确定删除吗？', {
                btn: ['确定', '取消'] //按钮
            }, function () {
                $.ajax({
                    type: 'post',
                    url: '../Back/DelType/' + DynamicId,
                    success: function () {
                        layer.msg("删除成功", { icon: 1 });
                        setTimeout(function () {
                            window.location.reload();
                        }, 2000);
                    },
                    error: function () {
                        layer.msg("删除失败", { icon: 2 });
                    }
                });
            }, function () {
                layer.msg('取消了删除', {
                    time: 10000, //10s后自动关闭
                    btn: ['明白了']
                });
            });
        }
    </script>
}



